package com.xaicode.learn.java.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * array blocking queue
 *
 * @author Locker cjxia@isoftstone.com
 * @version 1.0
 */
public class QueueArrayBlocking {

    public static void main(String[] args) throws InterruptedException {

        final BlockingQueue<String> bq = new ArrayBlockingQueue<>(16);

        for (int i = 0; i < 4; i++) {
            new Thread(() -> {
                while (true) {
                    try {
                        String log = bq.take();
                        parseLog(log);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        for (int i = 0; i < 16; i++) {
            String log = (i + 1) + " --> ";
            bq.put(log);
        }
    }

    public static void parseLog(String log) {
        System.out.println(log + System.currentTimeMillis());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
